<?php

namespace app\api\controller;

use app\api\model\Clipboard;
use app\api\model\Comment;
use app\api\model\PictureClipboard;
use app\common\controller\ApiController;
use think\App;
use app\api\model\Picture;
use think\Db;
use think\Request;

class ClipboardC extends ApiController
{

    /**
     * 画列表
     * @return string
     * @throws \Exception
     */
    public function list(Request $request)
    {
        $user = $this->getUserId();
        $list = Clipboard::where('user_id', $user['id'])->with(['pictureClipboard'=>function($q){
            $q->with(['picture'=>function($q){
                $q->limit(1);
            }]);
        }])->select();
        $this->success('',$list);
    }

    public function detail(Request $request)
    {
        $user = $this->getUserId();
        $cid = $request->param('cid');
        $detail = Clipboard::where('user_id', $user['id'])
            ->where('id',$cid)
            ->find();
        $this->success('',['detail' => $detail]);
    }
    
    public function add(Request $request){
        $user = $this->getUserId();
        $name = $request->param('name');
        $thumb = $request->param('thumb');
        $desc = $request->param('desc');
        $clipboard = new Clipboard();
        $clipboard->name = $name;
        $clipboard->user_id = $user['id'];
        $clipboard->desc = $desc;
        $clipboard->thumb = $thumb;
        $res = $clipboard->save();
        if($res){
            $this->success('添加成功');
        }else{
            $this->success('添加失败');
        }
    }

    public function edit(Request $request){
        $user = $this->getUserId();
        $name = $request->param('name');
        $thumb = $request->param('thumb');
        $desc = $request->param('desc');
        $cid = $request->param('cid');
        if(!$cid){
            $this->error('参数错误');
        }
        $clipboard = Clipboard::where('id', $cid)->find();
        $clipboard->name = $name;
        $clipboard->desc = $desc;
        $clipboard->thumb = $thumb;
        $res = $clipboard->save();
        if($res){
            $this->success('编辑成功');
        }else{
            $this->error('编辑失败');
        }
    }

    public function del(Request $request){
        $user = $this->getUserId();
        $cid = $request->param('cid');
        $cli = Clipboard::where('user_id', $user['user_id'])->where('id', $cid)->find();
        Db::startTrans();
        if($cli){
            Db::startTrans();
            try {
                PictureClipboard::where('cid',$cli['id'])->delete();
                Clipboard::where('user_id', $user['user_id'])->where('id', $cid)->delete();
            }catch(\Exception $exception){
                Db::rollback();
                $this->error('删除失败');
            }
            Db::commit();
            $this->success('删除成功');
        }else{
            $this->error('该画夹不存在');
        }
    }

    public function addtoC(Request $request){
        $user = $this->getUserId();
        $pid = $request->param('pid');
        $cid = $request->param('cid');
        $cinfo = PictureClipboard::where('pid', $pid)->where('cid', $cid)->where('user_id', $user['id'])->find();
        if(!empty($cinfo)){
            $this->error('已收藏在画夹');
        }
        $picCli = new PictureClipboard();
        $picCli->pid = $pid;
        $picCli->cid = $cid;
        $picCli->user_id = $user['id'];
        $res = $picCli->save();
        if($res){
            $this->success('收藏到画夹成功');
        }else{
            $this->error('操作失败');
        }
    }



}
